package com.zhy.learning.demo.lock;


public class SynchronizedTest {

    private static Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        int n = 10;
        Thread[] threads = new Thread[n];
        for (int i = 0; i < n; ++i) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (lock) {
                        System.out.println(Thread.currentThread().getName() + " get synchronized lock!");
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }

        synchronized (lock) {
            for (int i = 0; i < n; ++i) {
                threads[i].start();
                Thread.sleep(200);
            }
        }

        for (int i = 0; i < n; ++i) {
            threads[i].join();
        }
    }
}
